<?php

require_once 'BasicController.php';
require_once '../application/business/CartBusiness.php';

class CartController extends Zend_Controller_Action
{

	public function init()
	{
		$ad = new AdminBusiness();
		$user = Zend_Auth::getInstance()->getStorage()->read();
		$this->view->user = $user;


		if(isset($user) && $user != null){
			$cartBusiness = new CartBusiness();
			$this->view->shopping_count = $cartBusiness->countItems($user['idClient']);

		}

		$this->view->enterprises = $ad->getAllEnterprises();
		$this->view->categories = $ad->getAllCategories();

	}

	public function indexAction()
	{
		// action body
	}

	//MOSTRA OS ITENS NO CARRINHO DE UM DETERMINADO CLIENTE
	public function showCartAction(){
		$user = Zend_Auth::getInstance()->getStorage()->read();
		$cartBusiness = new CartBusiness();

		if(!Zend_Auth::getInstance()->getStorage()->read()) {
			echo "0";
			die();
		}

		if(isset($user) && $user != null){

			$cartBusiness = new CartBusiness();
			$this->view->shopping_cart = $cartBusiness->getCart($user['idClient']);

			foreach ($this->view->shopping_cart as $cart){
				$this->view->price = $cart['price']*$cart['qtd'] + $this->view->price;
			}
		}

	}

	//MOSTRA A TELA COM OS TIPOS DE PAGAMENTO/FRETE/ENDEREÇO
	public function showPaymentAction(){

		$addressBusiness = new AddressBusiness();
		$this->_helper->layout->disableLayout();

		$config = new Zend_Config_Ini('../application/configs/application.ini', 'production');

		$user = Zend_Auth::getInstance()->getStorage()->read();
		if(isset($user) && $user != null){
			$this->view->isHere = FALSE;
			$this->view->states = $addressBusiness->getAllStates();
			$cartBusiness = new CartBusiness();
			$clientBusiness = new ClientBusiness();
			$this->view->shopping_cart = $cartBusiness->getCart($user['idClient']);
			$this->view->client = $clientBusiness->getUserByid($user['idClient']);
			foreach ($this->view->shopping_cart as $cart){
				$this->view->price = $cart['price']*$cart['qtd'] + $this->view->price;

				$this->view->weight = $cart['weight'] + $this->view->weight;
			}

			if($this->view->weight < 0.300){
				$this->view->weight = 0.300;
			}
				
			$cep = $this->view->client['zipcode'];
			$city = $this->view->client['city'];
				
			$getCep = $this->getRequest()->getParam('cep');
			$getCity = $this->getRequest()->getParam('city');
				
			if($getCep){
				$cep = $getCep;
			}
				
			if($getCity){
				$city = $getCity;
			}
				
			$this->view->cities = $addressBusiness->getCityByState($this->view->client['state']);
				
			$idtCity = $addressBusiness->getCityByCepStreet($cep, $city);
				
			if($idtCity['idt_city'] == $config->idt->araraquara){
				$this->view->isHere = TRUE;
			}
		}
	}

	//ACTION PARA ALTERAR A QUANTIDADE DE UM DETERMINADO PRODUTO NO CARRINHO
	public function alterQtdAction(){
		$cartBusiness = new CartBusiness();

		$idCart = $this->_request->getParam('idCart');

		$qtd = $this->_request->getParam('qtd');

		$cartBusiness->alterCart($idCart, $qtd);
	}

	//ACTION PARA ADICIONAR PRODUTO AO CARRINHO
	public function addCartAction(){

		$cartBusiness = new CartBusiness();

		if($user = Zend_Auth::getInstance()->getStorage()->read()) {
			$idProduct = $this->_request->getParam('idProduct');

			$prod = $cartBusiness->findProductById($idProduct);

			if($prod[0]['status'] == 1){
				$data = array("id_client"  => $user['idClient'],
						   	  "id_product" => $idProduct,
						      "qtd" => 1,
				);

				$cartBusiness->addToCart($data);
			}else{
				echo '<div id="thanks">
							<div style="display: inline-block; border: 1px solid rgb(75, 2, 2); height: 250px; width: 100%; background: rgb(253, 213, 213); text-align: justify;">
								<div style="margin: 0 auto; height: 100px; width: 60%">
									<div style="float: left; background: url(/images/error.png); width: 50px; height: 50px; margin: 25px 0 0 10px;"></div>
									<div style="float: left; margin: 30px 0 0 10px; font-size: 40px">
										Erro ao adicionar produto!
									</div>
								</div>
								
								<div style="margin: 0 auto; width: 70%">
									Este produto não está disponível em nosso estoque no momento. Desulpe pelo transtorno.<br /><br />
									
									Agradecemos a preferência!
								</div>
							</div>
						</div>';
				//$this->_redirect('products');
			}
		} else {
			echo "0";
			die();
		}
	}

	//ACTION PARA DELETAR PRODUTO DO CARRINHO
	public function deleteCartAction(){

		$idCart = $this->_request->getParam('idCart');

		echo($idCart);

		$cartBusiness = new CartBusiness();

		$cartBusiness->deleteItems($idCart);

	}

	public function boletoAction() {
		$this->_helper->layout->disableLayout();
		$this->view->nome = $_GET['nome'];
		$this->view->valor = $_GET['valor'];
	}

}